//
//  JBDatePickerView.swift
//  joyBao
//
//  Created by Chen on 2017/7/27.
//  Copyright © 2017年 Chen. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift

class JBDatePickerView: UIView {

    fileprivate var selectCallback: ((_ date: String) -> ())?
    fileprivate var dismissCallback: (() -> Swift.Void)?
    fileprivate var tapGesture: UITapGestureRecognizer?
    fileprivate var containerView: UIView?
    fileprivate var toolBar: UIView?
    fileprivate var datePickerView: UIDatePicker?
    fileprivate let kToolBarHeight = 45
    fileprivate let kPickerViewHeight = 250
    
    func showPickerView(_ selectDate: String, selectCallback: ((_ title: String) -> ())?, dismissCallback: (() -> Swift.Void)?) {
        self.selectCallback = selectCallback
        self.dismissCallback = dismissCallback
        
        self.setupUI()
        
        if selectDate.lengthOfBytes(using: .utf8) > 0 {
            let selectedDate = selectDate.format(format: "yyyy-MM-dd")
            self.datePickerView?.setDate(selectedDate, animated: false)
        }
        self.showPickerView()
    }
    
    func setupUI() {
        weak var weakSelf = self
        
        self.tapGesture = UITapGestureRecognizer()
        self.tapGesture?.rx.event.subscribe({ (event: Event) in
            switch event {
            case .next(let tapGesture):
                let y = tapGesture.location(in: self).y
                if y < (weakSelf?.containerView?.top)! {
                    weakSelf?.dismissPickerView()
                }
            default: break
            }
        }).addDisposableTo(bag)
        self.addGestureRecognizer(self.tapGesture!)
        
        self.containerView = UIView(frame: CGRect(x: 0, y: 0, width: Int(self.width), height: kToolBarHeight + kPickerViewHeight))
        self.containerView?.top = ScreenHeight
        self.containerView?.backgroundColor = UIColor.white
        self.addSubview(self.containerView!)
        
        self.toolBar = UIView(frame: CGRect(x: 0, y: 0, width: Int((self.containerView?.width)!), height: kToolBarHeight))
        self.toolBar?.backgroundColor = UIColor.white
        self.containerView?.addSubview(self.toolBar!)
        
        let cancelBtn = UIButton(type: .system)
        cancelBtn.frame = CGRect(x: 10, y: 0, width: kToolBarHeight, height: kToolBarHeight)
        cancelBtn.setTitle("取消", for: .normal)
        cancelBtn.setTitleColor(Normal_Text_Color, for: .normal)
        cancelBtn.titleLabel?.font = Font(font: 16)
        cancelBtn.touchUpInsideCallback {
            weakSelf?.dismissPickerView()
        }
        self.toolBar?.addSubview(cancelBtn)
        
        let sureBtn = UIButton(type: .system)
        sureBtn.frame = CGRect(x: 0, y: 0, width: kToolBarHeight, height: kToolBarHeight)
        sureBtn.right = (self.toolBar?.width)! - 10
        sureBtn.setTitle("确定", for: .normal)
        sureBtn.setTitleColor(Nav_BG_Color, for: .normal)
        sureBtn.titleLabel?.font = Font(font: 16)
        sureBtn.touchUpInsideCallback {
            let selectDate = weakSelf?.datePickerView?.date.format(format: "yyyy-MM-dd")
            weakSelf?.selectCallback?(selectDate!)
            weakSelf?.dismissPickerView()
        }
        self.toolBar?.addSubview(sureBtn)
        
        let line = UIView(frame: CGRect(x: 0, y: CGFloat(kToolBarHeight) - OnePixelLineWidth, width: (self.toolBar?.width)!, height: OnePixelLineWidth))
        line.backgroundColor = App_Line_Color
        self.toolBar?.addSubview(line)
        
        self.datePickerView = UIDatePicker(frame: CGRect(x: 0, y: kToolBarHeight, width: Int(ScreenWidth), height: kPickerViewHeight))
        self.datePickerView?.datePickerMode = .date
        self.datePickerView?.rx.controlEvent(.valueChanged).subscribe({ (event) in
            if ((weakSelf?.datePickerView?.date)! < Date()) {
                weakSelf?.datePickerView?.setDate(Date(), animated: true)
            }
        }).addDisposableTo(bag)
        self.containerView?.addSubview(self.datePickerView!)
    }
    
    func showPickerView() {
        UIView.animate(withDuration: 0.25) {
            self.backgroundColor = UIColor.black.withAlphaComponent(0.3)
            self.containerView?.top = self.height - (self.containerView?.height)!
        }
    }
    
    func dismissPickerView() {
        UIView.animate(withDuration: 0.25, animations: {
            self.backgroundColor = UIColor.clear
            self.containerView?.top = self.height
            self.removeGestureRecognizer(self.tapGesture!)
        }) { (finished) in
            self.dismissCallback?()
        }
    }

}
